package com.hcbd.tdwt.util;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;



import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

public class FileUtils {
    public static void writeErrorFile(String filePath,String content) {
        // final File obbDir = this.getObbDir();
        try {
            String today = DateUtils.getDate();
            //String filePath = FileUtil.getLogFile();
            String info = "**********************************\n" ;
            String curTime = DateUtils.getSysDate();//��ǰʱ��----yyyy-MM-dd HH:mm:ss
            info += curTime + "\n" ;
            info += content + "\n";
            final File kmlFile = new File(filePath+"log_error_" +today+ ".txt");
            if (!kmlFile.exists()) {
                kmlFile.createNewFile();
            }
            RandomAccessFile randomAccessFile = new RandomAccessFile(kmlFile, "rw");
            randomAccessFile.seek(kmlFile.length());
            randomAccessFile.write(info.getBytes());
            randomAccessFile.close();
        } catch (Exception e) {
            Log.e("write2File:写入失败", "write2File()==>" + e);
        }
    }
    //将字节数转化为MB
    @SuppressLint("DefaultLocale")
    public static String byteToMBStr(long size){
        long kb = 1024;
        long mb = kb*1024;


        float f = (float) size/mb;
        return String.format(f > 100 ?"%.2f":"%.2f",f);

    }
    public static String getFileMbSize(String filePath){
        String mb="0kb";
        try{
            mb= byteToMBStr(getFileSize(filePath));
        }catch (Exception e){
            e.printStackTrace();
        }
        return mb;
    }
    public static void saveBitmapFile(Bitmap bitmap,String path){
        File file=new File(path);//将要保存图片的路径
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取指定文件大小
     *
     * @param filePath
     * @return
     * @throws Exception
     */
    private static int getFileSize(String  filePath) throws Exception {
        int size = 0;
        File file=new File(filePath);
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            Log.e("获取文件大小","文件不存在!");
        }
        return size;
    }
    public static int getPcmSeconds(String  filePath){
        int seconds=0;
        try {
            int kb = getFileSize(filePath);
            seconds=kb/16000;
        }catch (Exception e){
            e.printStackTrace();
        }
        return seconds;
    }
    public static Bitmap createScaleBitmap(Bitmap bmpSrc, int targetWidth, int targetHeight) {
        //Bitmap bmpSrc = BitmapFactory.decodeFile(path);
        int srcWidth = bmpSrc.getWidth();
        int srcHeight = bmpSrc.getHeight();
        float scale = 1;
        float widthScale = targetWidth * 1.0f / srcWidth;
        float heightScale = targetHeight * 1.0f / srcHeight;
        if(widthScale < heightScale){
            scale = widthScale;
        }else{
            scale = heightScale;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale, 0, 0);
        // 如需要可自行设置 Bitmap.Config.RGB_8888 等等
        Bitmap bmpRet = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bmpRet);
        // 如需要可自行设置 Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG 等等
        Paint paint = new Paint();
        canvas.drawBitmap(bmpSrc, matrix, paint);
        return bmpRet;
    }
    public static void writeFile(String filePath,String content) {
        // final File obbDir = this.getObbDir();
        try {
            String today = DateUtils.getDate();
            //String filePath = FileUtil.getLogFile();
            String info = "**********************************\n" ;
            String curTime = DateUtils.getSysDate();//��ǰʱ��----yyyy-MM-dd HH:mm:ss
            info += curTime + "\n" ;
            info += content + "\n";
            final File kmlFile = new File(filePath+"log_x_" +today+ ".txt");
            if (!kmlFile.exists()) {
                kmlFile.createNewFile();
            }
            RandomAccessFile randomAccessFile = new RandomAccessFile(kmlFile, "rw");
            randomAccessFile.seek(kmlFile.length());
            randomAccessFile.write(info.getBytes());
            randomAccessFile.close();
        } catch (Exception e) {
            Log.e("write2File:写入失败", "write2File()==>" + e);
        }
    }
    public static void writeFile(String filePath,String logName,String content) {
        // final File obbDir = this.getObbDir();
        try {
            String today = DateUtils.getDate();
            //String filePath = FileUtil.getLogFile();
            String info = "**********************************\n" ;
            String curTime = DateUtils.getSysDate();//��ǰʱ��----yyyy-MM-dd HH:mm:ss
            info += curTime + "\n" ;
            info += content + "\n";
            final File kmlFile = new File(filePath+logName+today+ ".txt");
            if (!kmlFile.exists()) {
                kmlFile.createNewFile();
            }
            RandomAccessFile randomAccessFile = new RandomAccessFile(kmlFile, "rw");
            randomAccessFile.seek(kmlFile.length());
            randomAccessFile.write(info.getBytes());
            randomAccessFile.close();
        } catch (Exception e) {
            Log.e("write2File:写入失败", "write2File()==>" + e);
        }
    }
}
